import numpy as np

def ten2mat(tensor, mode):
    return np.reshape(np.moveaxis(tensor, mode, 0), (tensor.shape[mode], -1), order = 'F')



X = np.random.random([3,4,2])
X[:, :, 0] = [[1,4,7,10],[2,5,8,11],[3,6,9,12]]
X[:, :, 1] = [[13,16,19,22],[14,17,20,23],[15,18,21,24]]
print(X)



# X = np.array([[[1, 2, 3, 4], [3, 4, 5, 6]],
#               [[5, 6, 7, 8], [7, 8, 9, 10]],
#               [[9, 10, 11, 12], [11, 12, 13, 14]]])
print('tensor size:')
print(X.shape)
print()
print('切片矩阵：X[:, :, 1] =')
print(X[:, :, 0])
print()
print('切片矩阵：X[:, :, 2] =')
print(X[:, :, 1])
print()
# print('切片矩阵：X[:, :, 3] =')
# print(X[:, :, 2])
# print()
# print('切片矩阵：X[:, :, 4] =')
# print(X[:, :, 3])
print()
print('模态1展开矩阵 (mode-1 tensor unfolding):')
print(ten2mat(X, 0))
print()
print('模态2展开矩阵 (mode-2 tensor unfolding):')
print(ten2mat(X, 1))
print('模态3展开矩阵 (mode-3 tensor unfolding):')
print(ten2mat(X, 2))